﻿using DocumentListModule.Views;
using Infrastructure;
using log4net;
using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;


namespace DocumentListModule
{
    /// <summary>
    /// Document list presenter module
    /// </summary>
    [Module(ModuleName = "DocumentListModule")]
    public class DocumentListModule : IModule
    {
        private readonly IRegionManager _regionManager;
        readonly IUnityContainer _container;

        /// <summary>
        /// Initializes a new instance of the <see cref="DocumentListModule"/> class.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="regionManager">The region manager.</param>
        public DocumentListModule(IUnityContainer container, IRegionManager regionManager)
        {
            _container = container;
            _regionManager = regionManager;
        }

        /// <summary>
        /// Registers the types and services.
        /// </summary>
        protected void RegisterTypesAndServices()
        {
            _container.RegisterType<IDocumentListView, DocumentListView>(new ContainerControlledLifetimeManager());
            _container.RegisterType<IDocumentListPresenter, DocumentListPresenter>(new ContainerControlledLifetimeManager());
        }

        /// <summary>
        /// Registers the views with regions.
        /// </summary>
        private void RegisterViewsWithRegions()
        {
            _regionManager.RegisterViewWithRegion(RegionNames.DocumentListRegion, () => _container.Resolve<IDocumentListPresenter>().View);
        }


        #region IModule Members

        /// <summary>
        /// Notifies the module that it has be initialized.
        /// </summary>
        public void Initialize()
        {
            ILog log = _container.Resolve<ILog>();
            log.Info("DocumentListModule loaded");

            RegisterTypesAndServices();
            RegisterViewsWithRegions();
        }

        #endregion
    }
}
